package stuttgart.uni.locationtracker;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

//Objects of this class are used to exchange data between Service, GPXDataSaver and Activity
public class TrackingData implements Parcelable, Serializable{
	
	
	public static final int USERNAMELENTH = 20;
	private byte[] id;
	private byte[] username;
	private long time = 0;
	private double latitude = 0;
	private double longitude = 0;
	private float bearing = 0;
	private float speed = 0;
	
	public static final Parcelable.Creator<TrackingData> CREATOR = new Parcelable.Creator<TrackingData>() {
		public TrackingData createFromParcel(Parcel in) {
			return new TrackingData(in);
		}

		public TrackingData[] newArray(int size) {
			return new TrackingData[size];
		}
	};

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void readFromParcel(Parcel in) {
		in.readByteArray(id);
		in.readByteArray(username);
		time = in.readLong();
		latitude = in.readDouble();
		longitude = in.readDouble();
		bearing = in.readFloat();
		speed = in.readFloat();
	}
	
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeByteArray(id);
		dest.writeByteArray(username);
		dest.writeLong(time);
		dest.writeDouble(latitude);
		dest.writeDouble(longitude);
		dest.writeFloat(bearing);
		dest.writeFloat(speed);
	}
	
	private TrackingData(Parcel in) {
        readFromParcel(in);
    }
	
	public TrackingData(String id, String username,long time,double latitude, double longitude,float bearing,float speed) {
		
		try {
			this.id = id.getBytes("UTF-16");
			this.username = shortenUsername(username).getBytes("UTF-16");
			this.time = time;
			this.latitude = latitude;
			this.longitude = longitude;
			this.bearing = bearing;
			this.speed = speed;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String shortenUsername(String username){
		StringBuffer sb = new StringBuffer(TrackingData.USERNAMELENTH);
		for(int i=0;i<TrackingData.USERNAMELENTH;i++){
			int len = username.length();
			if(i<len){
				sb.insert(i, username.charAt(i));
			}else{
				sb.insert(i,' ');
			}
		}
		return sb.toString();
	}

	public String getId() {
		try {
			return new String(id,"UTF-16");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String getUsername() {
		
		try {
			return new String(username,"UTF-16");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public long getTime() {
		return time;
	}

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public double getHeading() {
		return bearing;
	}

	public float getBearing() {
		return bearing;
	}

	public float getSpeed() {
		return speed;
	}
	
	
}
